/**
 *  *2010-10-18 下午03:15:55  add 
 */
package com.base.myproject.client.busi.payment;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.GroupCheckBoxSelectionModel;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridViewConfig;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupSummaryView;
import com.extjs.gxt.ui.client.widget.grid.SummaryColumnConfig;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator
 *2010-10-18
 * javazgw@gmail.com
 */
public class AccountPayable extends BaseJDBCPanel{
	BasePagingLoader<PagingLoadResult<BusiObject>> loder;
	SendServlet ss = new SendServlet();
	ColumnModel cm;
	ModelType type ;
	StoreInfo info;
	Grid<BusiObject> grid;
	public AccountPayable()
	{
		super();
		
		init();
	}
	public void initToolBarItems() {
		super.initToolBarItems();
		
		 getToolbarll().remove(sava);
	}
	public void init() {
		
		this.add(initWeigetx());


	}
	public Widget initWeigetx() {
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(createGrid());
		return vpanel;
	}
	
	GroupingStore<BusiObject>  store = null;

	GroupingStore<BusiObject>  getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	GroupingStore<BusiObject> createStore() {
		type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("upicode");
		type.getField("upicode").setType(Long.class);
		type.addField("copybusicode");
		type.getField("copybusicode").setType(String.class);
		type.addField("copyicode");
		type.getField("copyicode").setType(String.class);
		type.addField("cgicode");
		type.getField("cgicode").setType(String.class);
		type.addField("cgcode");
		type.getField("cgcode").setType(String.class);
		type.addField("modeltype");
		type.getField("modeltype").setType(String.class);
		type.addField("gcode");
		type.getField("gcode").setType(String.class);
		type.addField("qty");
		type.getField("qty").setType(Double.class);
		type.addField("qtyunit");
		type.getField("qtyunit").setType(String.class);
//		type.addField("qtp");
//		type.getField("qtp").setType(Double.class);
//		type.addField("qtpunit");
//		type.getField("qtpunit").setType(String.class);
		
		type.addField("bqty");
		type.getField("bqty").setType(Double.class);
		type.addField("bqtyunit");
		type.getField("bqtyunit").setType(String.class);
		type.addField("upric");
		type.getField("upric").setType(Double.class);
		type.addField("codename");
		
//		type.addField("type");
//		type.getField("type").setType(Long.class);
		 info = new StoreInfo();
		info.setTablename("null");
//		info.setKeyname(new String[] { "icode" });
		info.setSql(getsql());
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		//store = new BaseStore<BusiObject>(type, info,false);
		loder = ss.getBaseLoaderX(type, info);
		store = new GroupingStore<BusiObject>(loder);
		store.groupBy("codename");
		loder.load();
		return store;
	}



	Grid createGrid() {
		GroupCheckBoxSelectionModel cbs = new GroupCheckBoxSelectionModel();
		List<ColumnConfig > columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		SummaryColumnConfig C4 = new SummaryColumnConfig("upicode", "发票号", 80);
		columns.add(C4);
		SummaryColumnConfig C11 = new SummaryColumnConfig("codename", "发票号", 80);
		columns.add(C11);
		SummaryColumnConfig C5 = new SummaryColumnConfig("cgicode", "barcode", 80);
		columns.add(C5);
		SummaryColumnConfig C6 = new SummaryColumnConfig("cgcode", "商品名称", 80);
		columns.add(C6);
		SummaryColumnConfig C7 = new SummaryColumnConfig("modeltype", "商品规格", 80);
		columns.add(C7);
		SummaryColumnConfig C8 = new SummaryColumnConfig("gcode", "商品大类", 80);
		columns.add(C8);
		SummaryColumnConfig C9 = new SummaryColumnConfig("qty", "数量", 80);
		columns.add(C9);
		SummaryColumnConfig C10 = new SummaryColumnConfig("qtyunit", "单位", 80);
		columns.add(C10);
		
		SummaryColumnConfig C12 = new SummaryColumnConfig("bqty", "已经收款数量", 80);
		columns.add(C12);
		SummaryColumnConfig C13 = new SummaryColumnConfig("bqtyunit", "单位", 80);
		columns.add(C13);
//		ColumnConfig C11 = new ColumnConfig("qtp", "qtp", 80);
//		columns.add(C11);
//		ColumnConfig C12 = new ColumnConfig("qtpunit", "qtpunit", 80);
//		columns.add(C12);
		SummaryColumnConfig C14 = new SummaryColumnConfig("upric", "单价", 80);
		columns.add(C14);
		
		cm = new ColumnModel(columns);
		
		
		GroupSummaryView view = new GroupSummaryView();
		//view.setShowGroupedColumn(false);
		view.setShowDirtyCells(false);
		view.setForceFit(true);
		view.setGroupRenderer(new GridGroupRenderer() {
			public String render(GroupColumnData data) {
				String f = cm.getColumnById(data.field).getHeader();
				String l = data.models.size() == 1 ? "条" : "条";
				return f + ": " + data.group + " (" + data.models.size() + " "
						+ l + ")";
			}
		});
		view.setShowGroupedColumn(true);
		
		
		grid = new Grid(getStore(), cm);
		
		GridViewConfig gvc = new GridViewConfig()
		{
			public String getRowStyle(ModelData model, int rowIndex, ListStore<ModelData> ds) {
			
//				if(model.get("type").equals("-1"))
//				{
//					return "gridviewbackgroup";
//				}
				return "";
			  }

		};
		view.setViewConfig(gvc);

		
		cbs.setSelectionMode(SelectionMode.MULTI);
		grid.addPlugin(cbs);
		grid.setView(view);
		grid.setLoadMask(true);
		grid.setSelectionModel(cbs);
		Tools.setResize(grid, 0.95	, 0.95);
		return grid;
	}

	
	private String getsql()
	{
		String str ="";
		str +=" select a.icode,a.upicode,a.codename,a.copybusicode,a.copyicode,a.cgicode,a.cgcode,a.modeltype,a.gcode,a.qty,a.qtyunit,a.upric,b.qty as bqty,b.qtyunit as bqtyunit from ";
		str +=" ( ";
		str +=" select *,(select codename from invoice where invoice.icode = invoiceg.upicode) as codename from invoiceg where exists(    ";
		str +=" select * from invoice where (invoiceg.upicode = invoice.icode and invoice.busistatus='131072' and invoicetype=1 and busicode='9910')";
		str +=" )   ";
		str +=" ) as a    ";
		str +=" left join ";
		str +=" (   ";
		str +=" select * from paymentsg where exists(";
		str +=" select * from payments where paymentsg.upicode = payments.icode and payments.busistatus = '131072' and payments.paytype = 1  ";
		str +=" )   ";
		str +=" ) as b    ";
		str +="";
		str +=" on a.cgicode = b.cgicode  ";
		return str;
	}
}
